#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

class Student {
public:
    string name="";
    int age;
    Student() {};

    Student(string n, int a) {
        name = n;
        age = a;
    }

    void info() {
        cout << "Age:" << age << " Name: " << &name << endl;
    }
};

void write_student(string filename, vector<Student> &obj_vec) {
    ofstream f_handle(filename, ios::out | ios::binary);
    for (auto obj:obj_vec) {
        f_handle.write((char *) &obj, sizeof(obj));
    }
    f_handle.close();

}

void read_student(string filename) {
    ifstream f_handle(filename, ios::in | ios::binary);
    Student s;
    if (!f_handle)
        cout << "Error" << endl;
    while (f_handle.read((char *) &s, sizeof(s))) {
        int readedBytes = f_handle.gcount();
        cout << "Readed " << readedBytes << " Bytes\n";
        s.info();
    }
    f_handle.close();
}

int main() {
    Student s1("Jack", 20);
    Student s2("Alice", 18);
    string filename = "student.dat";
    vector<Student> s_vec = vector<Student>();
    s_vec.push_back(s1);
    s_vec.push_back(s2);
//    write_student(filename, s_vec);
    read_student(filename);
    return 0;
}